package finalproject.mobapp.ARPackage;

import java.util.HashMap;
import java.util.Iterator;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;

public class MapOverlay extends Overlay {
	Context context;
	
	HashMap<Integer, Poi> poiLocations;
	
	Paint paint;
	Paint backPaint;
	
	static int markerRadius = 7;
	
	/**
	 * 
	 * @param _context
	 */
	public MapOverlay(Context _context) {
		super();
		
		context = _context;
		init();
	}
	
	public void init() {
		poiLocations = new HashMap<Integer, Poi>();
		
		backPaint = new Paint();
		backPaint.setARGB(200, 200, 200, 200);
		backPaint.setAntiAlias(true);
		
		paint = new Paint();
		paint.setARGB(255, 10, 10, 255);
		paint.setAntiAlias(true);
		paint.setFakeBoldText(true);
	}
	
	/**
	 * 
	 * @param _poiLocations
	 */
	public void setPoiLocations(HashMap<Integer, Poi> _poiLocations) {
		poiLocations = _poiLocations;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// Get the map projection to convert lat/long to screen coordinates
	    Projection projection = mapView.getProjection();
	    
	    // draw overlays
	    if (shadow == false) {
	    	if (poiLocations.size() > 0) {
	    		Iterator<Integer> e = poiLocations.keySet().iterator();
	    		do {
	    			int poi_id = e.next();
	    			Poi poi = poiLocations.get(poi_id);
	    			Location poi_location = poi.getLocation();
	    			
	    			// convert Location to GeoPoint
	    			Double latitude = poi_location.getLatitude()*1E6;
		    		Double longitude = poi_location.getLongitude()*1E6;
		    		GeoPoint geopoint = new GeoPoint(latitude.intValue(),longitude.intValue());
		    		
		    		// convert GeoPoint to screen coordinates
		    		Point point = new Point();
		    		point = projection.toPixels(geopoint, point);
		    		
		    		// draw point in poi location
		    		RectF oval = new RectF(point.x-markerRadius,
    						point.y-markerRadius,
    						point.x+markerRadius,
    						point.y+markerRadius);
		    		canvas.drawOval(oval, backPaint);
		    		oval.inset(2, 2);
		    		canvas.drawOval(oval, paint);
		    		
		    		// Draw the poi_title next to their position.
    				float textWidth = paint.measureText(poi.getTitle());
    				float textHeight = paint.getTextSize();
    				RectF textRect = new RectF(point.x+markerRadius, point.y-textHeight,
    						point.x+markerRadius+8+textWidth, point.y+4);
    				canvas.drawRoundRect(textRect, 3, 3, backPaint);
    				canvas.drawText(poi.getTitle(), point.x+markerRadius+4, point.y, paint);
	    		} while (e.hasNext());
	    		super.draw(canvas, mapView, shadow);
	    	}
	    }
	}
}
